<template>
  <div class="cart-container">
    <h2>购物车</h2>
    <div v-if="cart.length === 0">购物车为空</div>
    <div v-else>
      <div class="cart-list">
        <div v-for="item in cart" :key="item.id" class="cart-card">
          <img :src="item.img || `/src/assets/tea/longjing.jpg`" :alt="item.name" />
          <div class="cart-info">
            <div class="cart-title">{{ item.name }}</div>
            <div class="cart-price">¥{{ item.price }}</div>
            <div class="cart-qty">数量：{{ item.quantity }}</div>
            <button @click="removeItem(item.id)">移除</button>
          </div>
        </div>
      </div>
      <div class="cart-total">总价：¥{{ totalPrice }}</div>
      <button class="cart-checkout" @click="checkout">结算</button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import axios from '../api/axios';
import { useRouter } from 'vue-router';

const cart = ref<any[]>(JSON.parse(localStorage.getItem('cart') || '[]'));
const totalPrice = computed(() => cart.value.reduce((sum, item) => sum + item.price * item.quantity, 0));
const router = useRouter();

const removeItem = (id: string) => {
  cart.value = cart.value.filter(item => item.id !== id);
  localStorage.setItem('cart', JSON.stringify(cart.value));
};

const checkout = async () => {
  if (cart.value.length === 0) return;
  try {
    // 假设后端 /order 支持批量商品结算
    const token = localStorage.getItem('token');
    if (!token) {
      alert('请先登录');
      router.push('/login');
      return;
    }
    const res = await axios.post('/order', {
      items: cart.value.map(item => ({ teaId: item.id, quantity: item.quantity, price: item.price })),
      totalPrice: totalPrice.value
    });
    alert('结算成功，订单已生成！');
    cart.value = [];
    localStorage.removeItem('cart');
    router.push('/orders');
  } catch (e: any) {
    alert(e.response?.data?.message || '结算失败');
  }
};
</script>

<style scoped>
.cart-container {
  max-width: 900px;
  margin: 40px auto;
  padding: 32px;
  background: #fffbe6;
  border-radius: 16px;
  box-shadow: 0 2px 16px #ffd591;
}
.cart-list {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
  justify-content: flex-start;
}
.cart-card {
  display: flex;
  align-items: center;
  width: 400px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 8px #ffd591;
  padding: 16px;
  .cart-container {
    max-width: 1200px;
    margin: 40px auto;
    padding: 32px;
    background: #fffbe6;
    border-radius: 16px;
    box-shadow: 0 4px 24px #ffe7ba;
    min-height: 480px;
  }
}
.cart-info {
  flex: 1;
}
.cart-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 4px;
}
.cart-price {
  color: #ff6600;
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 4px;
}
.cart-qty {
  font-size: 14px;
  margin-bottom: 8px;
}
.cart-card button {
  background: #ff6600;
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 6px 16px;
  cursor: pointer;
}
.cart-total {
  font-weight: bold;
  margin: 24px 0 16px 0;
  font-size: 20px;
}
.cart-checkout {
  background: #ff6600;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 12px 32px;
  font-size: 18px;
  cursor: pointer;
}
</style>
